#!/bin/bash
set -e

source $(dirname $0)/version
source $(dirname $0)/images

cd $(dirname $0)/..

DIST=$(pwd)/dist/artifacts

pushd images/output
build_all "$@"
mkdir -p ${DIST}
copy_all ${DIST} "$@"
popd

mkdir -p ./build
ID=$(docker create ${REPO}/k3os-package:${TAG})
docker cp ${ID}:/output/k3os ./build/
docker rm -fv $ID

docker build \
  --build-arg ARCH=${ARCH} \
  --build-arg REPO=${REPO} \
  --build-arg TAG=${TAG} \
  --build-arg VERSION=${VERSION} \
  --file package/Dockerfile \
  --tag ${REPO}/k3os:${TAG} \
  --tag ${REPO}/k3os:latest \
.
docker image save --output ./dist/images.tar \
  ${REPO}/k3os:${TAG} \
  ${REPO}/k3os:latest
echo ${REPO}/k3os:${TAG} > ./dist/images.txt
echo Built ${REPO}/k3os:${TAG}
